package cn.bigfire.wedesk.net.codec;

import cn.bigfire.wedesk.bean.Wedesk;
import cn.bigfire.wedesk.net.protocol.Protocol;
import cn.bigfire.wedesk.net.protocol.TypeMapper;
import cn.bigfire.wedesk.util.ByteUtil;
import cn.bigfire.wedesk.util.ProtostuffUtil;
import com.alibaba.fastjson.JSON;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;

import java.util.List;

/**
 * 业务层 编解码器
 */
public class ProtocolWebSocketDecoder extends MessageToMessageDecoder<WebSocketFrame> {

    @Override
    protected void decode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception {
        try {
            Protocol protocol = new Protocol();
            if (msg instanceof TextWebSocketFrame) {
                TextWebSocketFrame textWebSocketFrame = (TextWebSocketFrame) msg;
                protocol = JSON.parseObject(textWebSocketFrame.text(), Protocol.class);
                if (protocol.getCmd() != Wedesk.Cmd.CMD_SCREENSHOT_DATA.getValue()){
                    System.out.printf("<-cmd:%s,type:%s,len:%s[%s]%n", protocol.getCmd(), protocol.getType(), textWebSocketFrame.text().length(), ByteUtil.size(textWebSocketFrame.text().length()));
                }
//                System.out.printf("<-cmd:%s,type:%s,len:%s[%s]%n", protocol.getCmd(), protocol.getType(), textWebSocketFrame.text().length(), ByteUtil.size(textWebSocketFrame.text().length()));
                if (protocol.getCmd() != 0 && protocol.getObj() != null) {//有效数据再往下传递
                    out.add(protocol);
                }
            } else if (msg instanceof BinaryWebSocketFrame) {
                BinaryWebSocketFrame binaryWebSocketFrame = (BinaryWebSocketFrame) msg;
                ByteBuf content = binaryWebSocketFrame.content();
                int cmd = content.readInt();
                int type = content.readInt();
                byte[] bytes = new byte[content.readableBytes()];
                content.readBytes(bytes);
                protocol.setCmd(cmd);
                protocol.setType(type);
                Class<?> codec = TypeMapper.getCodec(type);
                if (codec != null) {
                    protocol.setObj(ProtostuffUtil.deserializer(bytes, codec));
                } else {
                    protocol.setObj(bytes);
                }
                if (protocol.getCmd() != 0 && protocol.getObj() != null) {//有效数据再往下传递
                    if (protocol.getCmd() != Wedesk.Cmd.CMD_SCREENSHOT_DATA.getValue()){
                        System.out.printf("<-cmd:%s,type:%s,len:%s[%s]%n", protocol.getCmd(), protocol.getType(), bytes.length, ByteUtil.size(bytes.length));
                    }
//                    System.out.printf("<-cmd:%s,type:%s,len:%s[%s]%n", protocol.getCmd(), protocol.getType(), bytes.length, ByteUtil.size(bytes.length));
                    out.add(protocol);
                }
            } else {
                System.err.println("不能解码的类型" + msg.getClass().getName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
